home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / T U R B O Language / Turbo C Tools v6.0 / EXAMPLES / NOANSI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  5.9 KB  |  261 lines

  1. /**
  2. *    NOANSI.C    TSR to allow enabling or disabling of ANSI.SYS.
  3. *
  4. *  The command line format is as follows:
  5. *
  6. *    noansi [-r]
  7. *
  8. *  NOANSI is a self-removing TSR. If it is invoked with no arguments, it
  9. *  installs itself (if not already installed). The -r option will cause
  10. *  it to remove itself if it is installed.
  11. *
  12. *  Version    6.00 (C)Copyright Blaise Computing Inc. 1989
  13. *
  14. **/
  15.  
  16.  
  17. #include <dos.h>
  18. #include <stdio.h>
  19.  
  20. #include <binterv.h>
  21. #include <bintrupt.h>
  22. #include <bkeybrd.h>
  23. #include <bkeys.h>
  24. #include <butil.h>
  25. #include <ctype.h>
  26.  
  27.         /* The declaration of the intervention function.    */
  28.         /* It will be called when the hot key is pressed.   */
  29. void keyhandler(IV_EVENT *);
  30.  
  31. #define TRUE  1
  32. #define FALSE 0
  33. #define NUL   '\0'
  34.  
  35. #define STKSIZE  2000
  36. #define OK     0
  37. #define FALL_OUT 101
  38.  
  39. #define NOT_FOUND      1
  40. #define ERROR_DISABLING   2
  41. #define ERROR_REMOVING      3
  42. #define ALREADY_INSTALLED 4
  43. #define NO_ANSI_INSTALLED 5
  44.  
  45.         /* Signature for this version of CTRLANSI.        */
  46. #define NOANSI_SIGN "NOANSI  03/31/89"
  47.  
  48.         /* Allocation of stack space for the intervention   */
  49.         /* scheduler and user function.             */
  50. char schedstk [STKSIZE];
  51.  
  52.         /* This is the data structure to pass to the        */
  53.         /* scheduler so that it will give control every     */
  54.         /* time ALT A is pressed.                */
  55. IV_KEY    keytab [] =
  56. {
  57.     {KB_C_A_A,        KB_S_A_A,       IV_KY_SERVICE}, /* Activate ANSI */
  58.     {KB_C_A_D,        KB_S_A_D,       IV_KY_SERVICE}  /* Disable ANSI  */
  59. };
  60.  
  61. #define k(c) kbscanof(c)
  62.  
  63.         /* Internal functions -- install and remove        */
  64.         /* intervention function.                */
  65. int install_iv(void);
  66. int remove_iv(void);
  67.  
  68. void main(int, char **);
  69.  
  70. void main(argc, argv)
  71. int   argc;
  72. char *argv[];
  73. {
  74.     if (argc == 1)
  75.     exit(install_iv());
  76.  
  77.     if ((argc == 2) && ((argv[1][0] == '-') || (argv[1][0] == '/')))
  78.     if (toupper(argv [1][1]) == 'R')
  79.         exit (remove_iv());
  80.  
  81.     printf ("usage: ctrlansi [-r]\n");
  82.     exit (0);
  83. }
  84.  
  85.  
  86.  
  87. /**
  88. *
  89. * Name        INSTALL_IV -- Install interrupt vectors for NOANSI,
  90. *                  then terminate and stay resident.
  91. *
  92. * Synopsis    ret = install_iv();
  93. *
  94. *        int ret     Return code from IVINSTAL if an
  95. *                error condition was encountered.
  96. *
  97. * Description    This function installs NOANSI if another copy
  98. *        is not already installed, and ANSI.SYS is installed.
  99. *
  100. * Returns    ret    ALREADY_INSTALLED (4)
  101. *                A copy of CTRLANSI is already installed.
  102. *            NO_ANSI_INSTALLED (5)
  103. *                ANSI.SYS is not installed.
  104. *            FALL_OUT (101)
  105. *                ISRESEXT() failed.
  106. *
  107. **/
  108.  
  109. int install_iv()
  110. {
  111.     int        ercode;
  112.     IV_VECTORS vecs;
  113.  
  114.         /* Check to see if CTRLANSI already installed.        */
  115.     ivvecs(IV_RETVEC, &vecs);
  116.     if (ivsense(&vecs, NOANSI_SIGN) != FARNIL)
  117.     {
  118.     puts ("NOANSI already installed.");
  119.     return(ALREADY_INSTALLED);
  120.     }
  121.  
  122.         /* Check to see if ANSI.SYS is installed.        */
  123.     if (UT_ANS_ABSENT == utansi(UT_ANS_DETECT))
  124.     {
  125.     puts("ANSI.SYS is not installed.\n");
  126.     return(NO_ANSI_INSTALLED);
  127.     }
  128.  
  129.         /* Install the intervention routine.            */
  130.     ercode = ivinstal(keyhandler, NOANSI_SIGN, schedstk, STKSIZE,
  131.               keytab, sizeof(keytab) / sizeof(IV_KEY),
  132.               NIL, 0,
  133.         /* utansi() uses DOS functions, including 1-12.     */
  134.               IV_DOS_NEED | IV_DKEY_NEED | IV_NO_FLOAT_NEED);
  135.     if (ercode != 0)
  136.     {
  137.     printf("IVINSTAL error %d.\n", ercode);
  138.     return(ercode);
  139.     }
  140.  
  141.         /* Terminate and stay resident.             */
  142.     isresext(OK);
  143.  
  144.         /* Should never get here.                */
  145.     return(FALL_OUT);
  146. }
  147.  
  148.  
  149.  
  150. /**
  151. *
  152. * Name        REMOVE_IV -- Remove a previously installed copy of
  153. *                 NOANSI.
  154. *
  155. * Synopsis    ret = remove_iv();
  156. *
  157. *        int ret     Return code.
  158. *                  NO_ERROR  (0)-
  159. *                      No error encountered.
  160. *                  NOT_FOUND (1)-
  161. *                      NOANSI not found.
  162. *                  ERROR_DISABLING (2)-
  163. *                      NOANSI could not be disabled.
  164. *                      This error should *never* be
  165. *                      seen.
  166. *                  ERROR_REMOVING (3)--
  167. *                      NOANSI could not be removed (most
  168. *                      likely overwritten MALLOC
  169. *                      pointers).
  170. *
  171. * Description    This function removes a currently-active copy of NOANSI
  172. *        from memory, restoring interrupt vectors and freeing
  173. *        memory in the process.
  174. *
  175. * Returns    ret (nonzero if error--see above).
  176. *
  177. **/
  178.  
  179. int remove_iv()
  180. {
  181.     IV_VECTORS     vecs;
  182.     IV_CTRL far *pivctrl;
  183.  
  184.         /* Check to see if NOANSI installed.          */
  185.     ivvecs(IV_RETVEC, &vecs);
  186.     if ((pivctrl = ivsense(&vecs, NOANSI_SIGN)) == FARNIL)
  187.     {
  188.     puts("NOANSI not found.");
  189.     return(NOT_FOUND);
  190.     }
  191.  
  192.         /* Make sure to leave ANSI.SYS enabled on exit.     */
  193.     utansi(UT_ANS_ENABLE);
  194.  
  195.     if (ivdisabl(pivctrl))
  196.     {
  197.     puts("Error disabling NOANSI.");
  198.     return(ERROR_DISABLING);
  199.     }
  200.  
  201.     if (isremove(pivctrl->psp))
  202.     {
  203.     puts("Error removing NOANSI.");
  204.     return(ERROR_REMOVING);
  205.     }
  206.  
  207.     return(0);
  208. }
  209.  
  210.  
  211.  
  212. /**
  213. *
  214. * Name        KEYHANDLER -- Handle hot key events.
  215. *
  216. * Synopsis    (To be called only by intervention code scheduler)
  217. *
  218. *        keyhandler(pevent);
  219. *
  220. *        IV_EVENT *pevent    Pointer to intervention event
  221. *                    structure.  The structure
  222. *                    contains the hot key which
  223. *                    was detected (if any), as
  224. *                    well as other data.
  225. *
  226. * Description    This function accepts control from the scheduler
  227. *        every time a defined hot key is pressed.  ALT-A will
  228. *        disable ANSI.SYS; ALT-D will disable it.
  229. *
  230. * Returns    None.
  231. *
  232. **/
  233.  
  234. void keyhandler(pevent)
  235. IV_EVENT *pevent;
  236. {
  237.     int error;
  238.  
  239.         /* Do a key action, if one exists.            */
  240.     switch (pevent->key.action)
  241.     {
  242.         /* If the user typed a "service" key, perform the   */
  243.         /* appropriate action.                    */
  244.     case IV_KY_SERVICE:
  245.         if (pevent->key.keycode == KB_S_A_A)
  246.         {
  247.         error = utansi(UT_ANS_ENABLE);
  248.         if (error != UT_ANS_ENABLE)
  249.             printf("NOANSI: Error enabling ANSI.SYS.\n");
  250.         }
  251.         else
  252.         {
  253.         error = utansi(UT_ANS_DISABLE);
  254.         if (error != UT_ANS_DISABLE)
  255.             printf("NOANSI: Error disabling ANSI.SYS.\n");
  256.         }
  257.     break;
  258.     }
  259.  
  260. }
  261.